const bcrypt = require('bcryptjs')
const sequelize = require('sequelize/index')
const Op = sequelize.Op
/**
 * @description: 加密处理
 * @param password {原密码}
 * @param saltTimes {加盐次数}
 * @return: 加密信息
 */
const encrypt = async (password, saltTimes) => {
  const hash = await bcrypt.hashSync(password, saltTimes)
  console.log(hash)
  return hash
}

/**
 * @description: 密码验证
 * @param password 原密码
 * @param hash 加密密码
 * @return:
 */
const validate = async (password, hash) => {
  const match = await bcrypt.compare(password, hash)
  return match
}
// 定义接口返回的数据模型
class BaseModel {
  constructor(data, message) {
    if (typeof data === 'string') {
      this.message = data
      data = null
      message = null
    }
    if (data) {
      this.data = data
    }

    if (message) {
      this.message = message
    }
  }
}
/**
 * @name: 成功返回模块
 * @method:
 * @msg: 成功返回模块
 * @param {type}
 */
class SuccessModel extends BaseModel {
  constructor(data, message) {
    super(data, message)
    this.code = 0
  }
}

/**
 * @name: FengWQ
 * @method:
 * @msg: 失败返回模块
 * @param {type}
 */
class ErrorModel extends BaseModel {
  constructor(data, message) {
    super(data, message)
    this.code = 1
  }
}
/**
 * @name: FengWQ
 * @method:
 * @msg: 无权限返回模块
 * @param {type}
 */
class noAuthErrorModel extends BaseModel {
  constructor(data, message) {
    super(data, message)
    this.code = 401
  }
}
const queryFilter = params => {
  let filter = {}
  delete params['limit']
  delete params['page']
  Object.keys(params).map(item => {
    if (params[item]) {
      filter[item] = {
        [Op.like]: '%' +params[item] + '%'
      }
    }
  })
  return filter
}

const sonsTree = (data, id, id_name) => {
  function tree(id) {
    let arr = []
    data
      .filter(item => {
        return item.parent_id === id
      })
      .forEach(item => {
        const temp_item = JSON.parse(JSON.stringify(item))
        temp_item.children = tree(item[id_name])
        arr.push(temp_item)
      })
    arr.sort(function(val1, val2) {
      return val1.sort - val2.sort
    })
    return arr
  }
  return tree(id)
}
module.exports = {
  encrypt,
  validate,
  SuccessModel,
  noAuthErrorModel,
  ErrorModel,
  queryFilter,
  sonsTree
}
